/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import HTTPDownloader.HTTPTaskManager;
import Selenium.leech_buploads;
import Tools.Settings;
import Tools.leech_buploads_supp;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.JTable;

/**
 *
 * @author patricksamson236
 */
public class MainWindow extends javax.swing.JFrame {

    private Settings settings;
    private TableTaskManager tableTaskManager;
    private int lastTaskSelected = -1;

    /**
     * Creates new form MainWindow
     */
    public MainWindow() {

        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }

        initComponents();
    }

    public void setSettings(Settings settings) {
        this.settings = settings;
        tableTaskManager = new TableTaskManager(table_tasks);
    }

    public int getLastTaskSelected() {
        return lastTaskSelected;
    }

    public TableTaskManager getTableTaskManager() {
        return tableTaskManager;
    }
    
    public JTable getTableTasks(){
        return table_tasks;
    }

    public void updateSummaryPanel(Object[] list) {
        int i = 0;
        progress_bar.setValue((Integer) list[i++]);
        val_num_connections.setText((String) list[i++]);
        val_progress.setText((String) list[i++]);
        val_size_completed.setText((String) list[i++]);
        val_size_remaining.setText((String) list[i++]);
        val_size_total.setText((String) list[i++]);
        val_speed_down_avg.setText((String) list[i++]);
        val_speed_down_curr.setText((String) list[i++]);
        val_speed_up_avg.setText((String) list[i++]);
        val_speed_up_curr.setText((String) list[i++]);
        val_status.setText((String) list[i++]);
        val_time_created.setText((String) list[i++]);
        val_time_completed.setText((String) list[i++]);
        val_time_elapsed.setText((String) list[i++]);
        val_time_remaining.setText((String) list[i++]);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        dialog_add_http_task = new javax.swing.JDialog();
        jPanel2 = new javax.swing.JPanel();
        lbl_http_url = new javax.swing.JLabel();
        txt_http_url = new javax.swing.JTextField();
        btn_http_cancel = new javax.swing.JButton();
        btn_http_download = new javax.swing.JButton();
        jSplitPane1 = new javax.swing.JSplitPane();
        jSplitPane2 = new javax.swing.JSplitPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        table_tasks = new javax.swing.JTable();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        scroll_summary = new javax.swing.JScrollPane();
        pnl_summary = new javax.swing.JPanel();
        progress_bar = new javax.swing.JProgressBar();
        pnl_right = new javax.swing.JPanel();
        txt_num_connections = new javax.swing.JLabel();
        txt_time_elapsed = new javax.swing.JLabel();
        txt_speed_down_avg = new javax.swing.JLabel();
        txt_speed_up_avg = new javax.swing.JLabel();
        txt_size_completed = new javax.swing.JLabel();
        txt_time_completed = new javax.swing.JLabel();
        txt_progress = new javax.swing.JLabel();
        val_speed_down_avg = new javax.swing.JLabel();
        val_time_elapsed = new javax.swing.JLabel();
        val_num_connections = new javax.swing.JLabel();
        val_speed_up_avg = new javax.swing.JLabel();
        val_size_completed = new javax.swing.JLabel();
        val_progress = new javax.swing.JLabel();
        val_time_completed = new javax.swing.JLabel();
        pnl_left = new javax.swing.JPanel();
        txt_status = new javax.swing.JLabel();
        txt_time_remaining = new javax.swing.JLabel();
        txt_speed_down_curr = new javax.swing.JLabel();
        txt_speed_up_curr = new javax.swing.JLabel();
        txt_size_remaining = new javax.swing.JLabel();
        txt_size_total = new javax.swing.JLabel();
        txt_time_created = new javax.swing.JLabel();
        val_speed_down_curr = new javax.swing.JLabel();
        val_time_remaining = new javax.swing.JLabel();
        val_status = new javax.swing.JLabel();
        val_speed_up_curr = new javax.swing.JLabel();
        val_size_remaining = new javax.swing.JLabel();
        val_size_total = new javax.swing.JLabel();
        val_time_created = new javax.swing.JLabel();
        scroll_stats = new javax.swing.JScrollPane();
        jPanel1 = new javax.swing.JPanel();
        jMenuBar1 = new javax.swing.JMenuBar();
        file = new javax.swing.JMenu();
        open_torrent_file = new javax.swing.JMenuItem();
        open_magnet_link = new javax.swing.JMenuItem();
        create_torrent = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        new_http_task = new javax.swing.JMenuItem();
        tools = new javax.swing.JMenu();
        task = new javax.swing.JMenu();
        task_resume = new javax.swing.JMenuItem();
        task_stop = new javax.swing.JMenuItem();
        task_remove = new javax.swing.JMenuItem();

        dialog_add_http_task.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        dialog_add_http_task.setTitle("Add new HTTP download task");
        dialog_add_http_task.setAlwaysOnTop(true);
        dialog_add_http_task.setMinimumSize(new java.awt.Dimension(400, 200));
        dialog_add_http_task.setName("new_http_task"); // NOI18N
        dialog_add_http_task.setResizable(false);

        lbl_http_url.setText("Enter a valid URL :");

        txt_http_url.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txt_http_urlKeyPressed(evt);
            }
        });

        btn_http_cancel.setText("Cancel");
        btn_http_cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_http_cancelActionPerformed(evt);
            }
        });

        btn_http_download.setText("Download");
        btn_http_download.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_http_downloadActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addGap(0, 227, Short.MAX_VALUE)
                        .addComponent(btn_http_cancel)
                        .addGap(18, 18, 18)
                        .addComponent(btn_http_download))
                    .addComponent(txt_http_url, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lbl_http_url, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lbl_http_url)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txt_http_url, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 77, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btn_http_download)
                    .addComponent(btn_http_cancel))
                .addContainerGap())
        );

        javax.swing.GroupLayout dialog_add_http_taskLayout = new javax.swing.GroupLayout(dialog_add_http_task.getContentPane());
        dialog_add_http_task.getContentPane().setLayout(dialog_add_http_taskLayout);
        dialog_add_http_taskLayout.setHorizontalGroup(
            dialog_add_http_taskLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        dialog_add_http_taskLayout.setVerticalGroup(
            dialog_add_http_taskLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Scitner");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jSplitPane1.setDividerLocation(200);

        jSplitPane2.setDividerLocation(290);
        jSplitPane2.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);

        table_tasks.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Name", "Size", "Progress", "Remaining Time", "Download Speed", "Upload Speed", "Size Remaining"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        table_tasks.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_LAST_COLUMN);
        table_tasks.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        table_tasks.setDoubleBuffered(true);
        table_tasks.setDragEnabled(true);
        table_tasks.setSurrendersFocusOnKeystroke(true);
        table_tasks.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                table_tasksMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(table_tasks);

        jSplitPane2.setTopComponent(jScrollPane1);

        progress_bar.setStringPainted(true);

        txt_num_connections.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        txt_num_connections.setText("Number of connections : ");

        txt_time_elapsed.setText("Elapsed time : ");

        txt_speed_down_avg.setText("Average download speed : ");

        txt_speed_up_avg.setText("Average upload speed : ");

        txt_size_completed.setText("Bytes downloaded : ");

        txt_time_completed.setText("Task completed on : ");

        txt_progress.setText("Progress : ");

        val_speed_down_avg.setText("-");
        val_speed_down_avg.setPreferredSize(new java.awt.Dimension(160, 14));

        val_time_elapsed.setText("-");
        val_time_elapsed.setPreferredSize(new java.awt.Dimension(160, 14));

        val_num_connections.setText("-");
        val_num_connections.setPreferredSize(new java.awt.Dimension(160, 14));

        val_speed_up_avg.setText("-");
        val_speed_up_avg.setPreferredSize(new java.awt.Dimension(160, 14));

        val_size_completed.setText("-");
        val_size_completed.setPreferredSize(new java.awt.Dimension(160, 14));

        val_progress.setText("-");
        val_progress.setPreferredSize(new java.awt.Dimension(160, 14));

        val_time_completed.setText("-");
        val_time_completed.setPreferredSize(new java.awt.Dimension(160, 14));

        javax.swing.GroupLayout pnl_rightLayout = new javax.swing.GroupLayout(pnl_right);
        pnl_right.setLayout(pnl_rightLayout);
        pnl_rightLayout.setHorizontalGroup(
            pnl_rightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnl_rightLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnl_rightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txt_speed_down_avg)
                    .addComponent(txt_time_elapsed)
                    .addComponent(txt_num_connections)
                    .addComponent(txt_speed_up_avg)
                    .addComponent(txt_size_completed)
                    .addComponent(txt_progress)
                    .addComponent(txt_time_completed))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnl_rightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(val_time_completed, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(val_progress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(val_size_completed, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(val_speed_up_avg, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(val_num_connections, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(val_time_elapsed, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(val_speed_down_avg, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pnl_rightLayout.setVerticalGroup(
            pnl_rightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnl_rightLayout.createSequentialGroup()
                .addGroup(pnl_rightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txt_num_connections)
                    .addComponent(val_num_connections, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnl_rightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txt_time_elapsed)
                    .addComponent(val_time_elapsed, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(5, 5, 5)
                .addGroup(pnl_rightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txt_speed_down_avg)
                    .addComponent(val_speed_down_avg, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnl_rightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txt_speed_up_avg)
                    .addComponent(val_speed_up_avg, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnl_rightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txt_size_completed)
                    .addComponent(val_size_completed, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnl_rightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txt_progress)
                    .addComponent(val_progress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnl_rightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txt_time_completed)
                    .addComponent(val_time_completed, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(12, Short.MAX_VALUE))
        );

        txt_status.setText("Status : ");

        txt_time_remaining.setText("Remaining time : ");

        txt_speed_down_curr.setText("Current download speed : ");

        txt_speed_up_curr.setText("Current upload speed : ");

        txt_size_remaining.setText("Bytes remaining : ");

        txt_size_total.setText("Task size : ");

        txt_time_created.setText("Task created on :");

        val_speed_down_curr.setText("-");
        val_speed_down_curr.setPreferredSize(new java.awt.Dimension(160, 14));

        val_time_remaining.setText("-");
        val_time_remaining.setPreferredSize(new java.awt.Dimension(160, 14));

        val_status.setText("-");
        val_status.setMinimumSize(new java.awt.Dimension(40, 14));
        val_status.setPreferredSize(new java.awt.Dimension(160, 14));

        val_speed_up_curr.setText("-");
        val_speed_up_curr.setPreferredSize(new java.awt.Dimension(160, 14));

        val_size_remaining.setText("-");
        val_size_remaining.setPreferredSize(new java.awt.Dimension(160, 14));

        val_size_total.setText("-");
        val_size_total.setPreferredSize(new java.awt.Dimension(160, 14));

        val_time_created.setText("-");
        val_time_created.setPreferredSize(new java.awt.Dimension(160, 14));

        javax.swing.GroupLayout pnl_leftLayout = new javax.swing.GroupLayout(pnl_left);
        pnl_left.setLayout(pnl_leftLayout);
        pnl_leftLayout.setHorizontalGroup(
            pnl_leftLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnl_leftLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnl_leftLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txt_speed_down_curr)
                    .addComponent(txt_time_remaining)
                    .addComponent(txt_status)
                    .addComponent(txt_speed_up_curr)
                    .addComponent(txt_size_remaining)
                    .addComponent(txt_size_total)
                    .addComponent(txt_time_created))
                .addGap(18, 18, 18)
                .addGroup(pnl_leftLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(val_speed_down_curr, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(val_time_created, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(val_size_total, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(val_size_remaining, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(val_speed_up_curr, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(val_status, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(val_time_remaining, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        pnl_leftLayout.setVerticalGroup(
            pnl_leftLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnl_leftLayout.createSequentialGroup()
                .addGroup(pnl_leftLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txt_status)
                    .addComponent(val_status, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnl_leftLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txt_time_remaining)
                    .addComponent(val_time_remaining, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnl_leftLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txt_speed_down_curr)
                    .addComponent(val_speed_down_curr, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnl_leftLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txt_speed_up_curr)
                    .addComponent(val_speed_up_curr, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnl_leftLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txt_size_remaining)
                    .addComponent(val_size_remaining, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnl_leftLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txt_size_total)
                    .addComponent(val_size_total, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnl_leftLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txt_time_created)
                    .addComponent(val_time_created, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(0, 11, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout pnl_summaryLayout = new javax.swing.GroupLayout(pnl_summary);
        pnl_summary.setLayout(pnl_summaryLayout);
        pnl_summaryLayout.setHorizontalGroup(
            pnl_summaryLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnl_summaryLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnl_summaryLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnl_summaryLayout.createSequentialGroup()
                        .addComponent(pnl_left, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(pnl_right, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 204, Short.MAX_VALUE))
                    .addComponent(progress_bar, javax.swing.GroupLayout.DEFAULT_SIZE, 870, Short.MAX_VALUE))
                .addContainerGap())
        );
        pnl_summaryLayout.setVerticalGroup(
            pnl_summaryLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnl_summaryLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(progress_bar, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnl_summaryLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(pnl_right, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(pnl_left, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(53, Short.MAX_VALUE))
        );

        progress_bar.getAccessibleContext().setAccessibleName("progress bar");

        scroll_summary.setViewportView(pnl_summary);

        jTabbedPane1.addTab("Summary", scroll_summary);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 890, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 526, Short.MAX_VALUE)
        );

        scroll_stats.setViewportView(jPanel1);

        jTabbedPane1.addTab("Statistics", scroll_stats);

        jSplitPane2.setRightComponent(jTabbedPane1);
        jTabbedPane1.getAccessibleContext().setAccessibleName("Summary");

        jSplitPane1.setRightComponent(jSplitPane2);

        file.setText("File");

        open_torrent_file.setText("Open Torrent file");
        file.add(open_torrent_file);

        open_magnet_link.setText("Open Magnet link");
        file.add(open_magnet_link);

        create_torrent.setText("Create Torrent");
        file.add(create_torrent);
        file.add(jSeparator1);

        new_http_task.setFocusable(true);
        new_http_task.setLabel("New HTTP download");
        new_http_task.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                new_http_taskActionPerformed(evt);
            }
        });
        file.add(new_http_task);

        jMenuBar1.add(file);

        tools.setText("Edit");
        jMenuBar1.add(tools);

        task.setText("Task");

        task_resume.setText("Resume Task");
        task.add(task_resume);

        task_stop.setText("Stop Task");
        task_stop.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                task_stopActionPerformed(evt);
            }
        });
        task.add(task_stop);

        task_remove.setText("Remove Task");
        task_remove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                task_removeActionPerformed(evt);
            }
        });
        task.add(task_remove);

        jMenuBar1.add(task);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 1105, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 566, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void new_http_taskActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_new_http_taskActionPerformed
        dialog_add_http_task.setEnabled(true);
        dialog_add_http_task.setVisible(true);
        txt_http_url.setText("");
        txt_http_url.requestFocusInWindow();
    }//GEN-LAST:event_new_http_taskActionPerformed

    private void btn_http_downloadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_http_downloadActionPerformed
        URL url = null;
        try {
            // validate the URL
            url = new URL(txt_http_url.getText());
        } catch (MalformedURLException ex) {
            System.out.println("The given URL is not valid!");
        }

        //check the host
        System.out.println(url.getHost());
        HTTPTaskManager httpmanager;
        try {
            //check if supported by any known leech
            leech_buploads_supp.valueOf(url.getHost().replace('.', '$').toUpperCase());
            System.out.println("This host is supported! Enjoy the speed!");
            try {
                //create task
                httpmanager = new HTTPTaskManager(settings.getHttpNumWorkers(),
                        settings.getHttpBufferSize(),
                        new File(settings.getHttpDestination()));
                httpmanager.setTaskName(url.toExternalForm());
                
                //add it to the task list
                tableTaskManager.addHttpTask(httpmanager);
                
                try {
                    //submit link to leech
                    url = new URL(new leech_buploads().submitLink(url.toExternalForm()));
                    
                    //add it to the task manager
                    httpmanager.setSourceURL(url);
                    httpmanager.setTaskName(httpmanager.getFileName());
                    
                    //start task
                    httpmanager.start();
                } catch (MalformedURLException ex) {
                    System.out.println("Could not get the final download link!");
                }
            } catch (IOException ex) {
                System.out.println("Could not create the task!");
            }
        } catch (IllegalArgumentException ex) {
            System.out.println("Not a supported host, sorry! :(");
            try {
                //create regular task
                httpmanager = new HTTPTaskManager(settings.getHttpNumWorkers(),
                        settings.getHttpBufferSize(),
                        new File(settings.getHttpDestination()));
                httpmanager.setSourceURL(url);
                httpmanager.setTaskName(httpmanager.getFileName());
                
                //add to task list
                tableTaskManager.addHttpTask(httpmanager);
                httpmanager.addOutputGUI(this);
                
                //start the task
                httpmanager.start();
            } catch (IOException ex2) {
                System.out.println("Could not create the new task");
            }
        }
        //show task summary
        lastTaskSelected = tableTaskManager.getHttpTasksList().size() - 1;

        //check the file extension

        //close the window
        btn_http_cancel.doClick();
    }//GEN-LAST:event_btn_http_downloadActionPerformed

    private void btn_http_cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_http_cancelActionPerformed
        dialog_add_http_task.setEnabled(false);
        dialog_add_http_task.setVisible(false);
        dialog_add_http_task.dispose();
    }//GEN-LAST:event_btn_http_cancelActionPerformed

    private void table_tasksMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_table_tasksMouseClicked
        lastTaskSelected = table_tasks.rowAtPoint(evt.getPoint());
        tableTaskManager.updateTaskUIOnce(lastTaskSelected);
    }//GEN-LAST:event_table_tasksMouseClicked

    private void txt_http_urlKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txt_http_urlKeyPressed
        if (evt.getKeyChar() == '\n'){
            btn_http_download.doClick();
        }
    }//GEN-LAST:event_txt_http_urlKeyPressed

    private void task_stopActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_task_stopActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_task_stopActionPerformed

    private void task_removeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_task_removeActionPerformed
        tableTaskManager.removeHTTPTask(lastTaskSelected);
    }//GEN-LAST:event_task_removeActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        tableTaskManager.saveHttpTasks();
        System.out.println("tasks saved!");
    }//GEN-LAST:event_formWindowClosing

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new MainWindow().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_http_cancel;
    private javax.swing.JButton btn_http_download;
    private javax.swing.JMenuItem create_torrent;
    private javax.swing.JDialog dialog_add_http_task;
    private javax.swing.JMenu file;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JSplitPane jSplitPane2;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JLabel lbl_http_url;
    private javax.swing.JMenuItem new_http_task;
    private javax.swing.JMenuItem open_magnet_link;
    private javax.swing.JMenuItem open_torrent_file;
    private javax.swing.JPanel pnl_left;
    private javax.swing.JPanel pnl_right;
    private javax.swing.JPanel pnl_summary;
    private javax.swing.JProgressBar progress_bar;
    private javax.swing.JScrollPane scroll_stats;
    private javax.swing.JScrollPane scroll_summary;
    private javax.swing.JTable table_tasks;
    private javax.swing.JMenu task;
    private javax.swing.JMenuItem task_remove;
    private javax.swing.JMenuItem task_resume;
    private javax.swing.JMenuItem task_stop;
    private javax.swing.JMenu tools;
    private javax.swing.JTextField txt_http_url;
    private javax.swing.JLabel txt_num_connections;
    private javax.swing.JLabel txt_progress;
    private javax.swing.JLabel txt_size_completed;
    private javax.swing.JLabel txt_size_remaining;
    private javax.swing.JLabel txt_size_total;
    private javax.swing.JLabel txt_speed_down_avg;
    private javax.swing.JLabel txt_speed_down_curr;
    private javax.swing.JLabel txt_speed_up_avg;
    private javax.swing.JLabel txt_speed_up_curr;
    private javax.swing.JLabel txt_status;
    private javax.swing.JLabel txt_time_completed;
    private javax.swing.JLabel txt_time_created;
    private javax.swing.JLabel txt_time_elapsed;
    private javax.swing.JLabel txt_time_remaining;
    private javax.swing.JLabel val_num_connections;
    private javax.swing.JLabel val_progress;
    private javax.swing.JLabel val_size_completed;
    private javax.swing.JLabel val_size_remaining;
    private javax.swing.JLabel val_size_total;
    private javax.swing.JLabel val_speed_down_avg;
    private javax.swing.JLabel val_speed_down_curr;
    private javax.swing.JLabel val_speed_up_avg;
    private javax.swing.JLabel val_speed_up_curr;
    private javax.swing.JLabel val_status;
    private javax.swing.JLabel val_time_completed;
    private javax.swing.JLabel val_time_created;
    private javax.swing.JLabel val_time_elapsed;
    private javax.swing.JLabel val_time_remaining;
    // End of variables declaration//GEN-END:variables
}
